package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.Enum.ResultEnum;
import com.dongdongshop.core.SpecificPrint;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.TbContentCategoryServiceI;
import com.dongdongshop.service.TbContentServiceI;
import com.dongdongshop.util.OSSUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("tbContentController")
public class TbContentController {

    private Logger logger= LoggerFactory.getLogger(TbSellerController.class);

    @Reference
    private TbContentServiceI tbContentServiceI;

    @Reference
    private TbContentCategoryServiceI tbContentCategoryServiceI;

    //查询
    @RequestMapping("getFindAll")
    public String getFindAll(Model model){
        List<TbContent> tbContents = tbContentServiceI.getFindAll();
        model.addAttribute("tcs",tbContents);
        List<TbContentCategory> tbContentCategories =   tbContentCategoryServiceI.getFidAll();
        model.addAttribute("tgs",tbContentCategories);
        return "admin/content";
    }


    @RequestMapping(value = "uploadImage",method = RequestMethod.POST)
    @ResponseBody
    public SpecificPrint uploadImage(@RequestParam(value = "myFile") MultipartFile myFile){
        try {
            //返回上传后的url
            String url = OSSUtil.upload(myFile);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(url);
        } catch (IOException e) {
            logger.error(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }


    @RequestMapping(value = "uploadImages",method = RequestMethod.POST)
    @ResponseBody
    public SpecificPrint uploadImages(@RequestParam(value = "myFiles") MultipartFile myFiles){
        try {
            //返回上传后的url
            String url = OSSUtil.upload(myFiles);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(url);
        } catch (IOException e) {
            logger.error(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }


    //回显
    @RequestMapping("doUpdate")
    @ResponseBody
    public TbContent doUpdate(Long id){
        TbContent tbContent =  tbContentServiceI.getFidById(id);
        return tbContent;
    }

    //增加
    @RequestMapping("addTbContent")
    @ResponseBody
    public SpecificPrint addTbContent(TbContent tbContent){
        try {
            tbContentServiceI.addTbContent(tbContent);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",tbContent);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(tbContent);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }

    }

    //确认修改
    @RequestMapping("updateTbContent")
    @ResponseBody
    public SpecificPrint updateTbContent(TbContent tbContent){
        try {
            System.out.println(tbContent.getPic());
            System.out.println("123231312321");
            tbContentServiceI.updateTbContent(tbContent);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",tbContent);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(tbContent);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }

    }

    @RequestMapping("deleteTbContent")
    @ResponseBody
    public SpecificPrint deleteTbContent(String ids){
        //截取字符串
        String idss=ids.replaceAll("'","");
        String[] split = idss.split(",");
        //遍历出所有选中的ID
        List<Integer> list=new ArrayList<>();
        for (String s : split) {
            list.add(Integer.valueOf(s));
        }

        try {
            tbContentServiceI.deleteTbContent(list);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",list);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }
}
